﻿using MediatR;
using ProductManagement.Applcation.Command;
using ProductManagement.Domain.Dtos;
using ProductManagement.Domain.ProductsDomain;
using ProductManagement.Infrstructre.Imp;
using ProductManagement.Infrstructre.Interface;
using System.Collections.Generic;

namespace ProductManagement.Applcation.CommandHanlers
{
    public class ProductsTypeSetCommandHandlers : IRequestHandler<ProductsTypeSetCommand, List<ProductsTypeSetDto>>
    {
        protected readonly InterfaceProductsTypeRepository repository;

        public ProductsTypeSetCommandHandlers(InterfaceProductsTypeRepository repository)
        {
            this.repository = repository;
        }

        public async Task<List<ProductsTypeSetDto>> Handle(ProductsTypeSetCommand request, CancellationToken cancellationToken)
        {
            List<ProductsTypeSetDto> Pdto = new List<ProductsTypeSetDto>();
            if (request.id == 0)
            {
                var newlist = (await repository.GetAll()).Where(x => x.IsDel == false).Select(x => new ProductsTypeSetDto
                {
                    ProductTypeId = x.ProductTypeId,
                    PTClass = x.PTClass,
                    PFatherId = x.PFatherId
                }).ToList();
                Pdto = newlist;
                if (request.PTClass != null)
                {
                    Pdto = Pdto.Where(x => x.PTClass.Contains(request.PTClass)).ToList();
                }
                return Pdto;
            }
            else
            {
                var list = (await repository.GetAll()).ToList();
                var ids = request.id;
                var productsinfo = BuildTree(list, request.id);
                List<ProductsTypeSetDto> treeCategories = new List<ProductsTypeSetDto>();
                foreach (var item in productsinfo)
                {
                    var dto = (await repository.GetAll()).Where(x => x.ProductTypeId == item.ProductTypeId).Select(x => new ProductsTypeSetDto
                    {
                        PFatherId = x.PFatherId,
                        ProductTypeId = x.ProductTypeId,
                        PTClass = x.PTClass
                    }).ToList();
                    treeCategories.AddRange(dto);
                }
                var dtos = (await repository.GetAll()).Where(x => x.ProductTypeId == request.id).Select(x => new ProductsTypeSetDto
                {
                    PFatherId = x.PFatherId,
                    ProductTypeId = x.ProductTypeId,
                    PTClass = x.PTClass
                }).ToList();
                treeCategories.AddRange(dtos);
                Pdto = treeCategories;
                if (request.PTClass != null)
                {
                    Pdto = Pdto.Where(x => x.PTClass.Contains(request.PTClass)).ToList();
                }
                return Pdto;
            }
        }

        //查询当前以及父级的所有子类
        static List<ProductType> BuildTree(List<ProductType> list, int ids)
        {
            List<ProductType> result = new List<ProductType>();
            var childs = list.Where(c => c.PFatherId == ids && c.IsDel == false).ToList();
            foreach (var item in childs)
            {
                result.Add(item);
                result.AddRange(BuildTree(list, item.ProductTypeId));
            }
            return result;
        }
    }
}
